<?php

// @formatter:off
/**
 * @file WCComicImagesHolder.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-06-03 03:40:32 +0000 (Tue, 03 Jun 2014) $
 *
 * $Id: WCComicImagesHolder.php 112 2014-06-03 03:40:32Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/includes/holders/WCComicImagesHolder.php $
 */
// @formatter:on

/**
 * @class WCComicImagesHolder @todo doc
 */
class WCComicImagesHolder extends WCHolderGroup {
	/**
	 * @var string Class name of the objects to be held.
	 */
	protected $_PropertiesItemClass = "WCComicImage";
	/**
	 * @var string This is the prefix of every column on the table
	 * '%WCComic_images'.
	 */
	protected $_PropertiesPrefix = "cim_";
	/**
	 * @var string This is the name of the table represented by WCComicImage
	 * objects. In this case, '%WCComic_images'.
	 */
	protected $_PropertiesTable = "comic_images";
	/**
	 * @var string This property indicates that the column name is not the
	 * default value. This means it is 'cim_id' instead of 'cim_name'.
	 */
	protected $_PropertiesNameColumn = "path";
	//
	// Public methods.
	public function addEntry(WCComic &$comic, $position, $path) {
		$out = false;
//		$out = $this->getEntry($comic, $position, $path);
//
//		if(!$out) {
		//
			// @{
		$stmt = false;
		$stmtID = __CLASS__."::".__FUNCTION__;
		if($this->_db->has($stmtID)) {
			$stmt = $this->_db->get($stmtID);
		} else {
			$query = "insert\n";
			$query.= "        into {$this->_dbprefix}comic_images(\n";
			$query.= "                cim_group, cim_comic, cim_position, cim_path)\n";
			$query.= "        values (:group, :comic, :position, :path)\n";

			$stmt = $this->_db->prepare($stmtID, $query);
		}
		// @}

		$params = array(
			":group" => $comic->group,
			":comic" => $comic->id(),
			":position" => $position,
			":path" => $path
		);

		if($stmt->execute($params)) {
			$out = $this->item($this->_db->lastInsertId());
		}
//		}

		return $out;
	}
	public function getEntry(WCComic &$comic, $position, $path) {
		$out = false;

		//
		// @{
		$stmt = false;
		$stmtID = __CLASS__."::".__FUNCTION__;
		if($this->_db->has($stmtID)) {
			$stmt = $this->_db->get($stmtID);
		} else {
			$query = "select  cim_id\n";
			$query.= "from    {$this->_dbprefix}comic_images\n";
			$query.= "where   cim_group    = :group\n";
			$query.= " and    cim_comic    = :comic\n";
			$query.= " and    cim_position = :position\n";
			$query.= " and    cim_path     = :path\n";

			$stmt = $this->_db->prepare($stmtID, $query);
		}
		// @}

		$params = array(
			":group" => $comic->group,
			":comic" => $comic->id(),
			":position" => $position,
			":path" => $path
		);

		if($stmt->execute($params) && $stmt->rowCount() > 1) {
			$row = $stmt->fetch();
			$out = $this->item($row["cim_id"]);
		}

		return $out;
	}
	public function itemsByComic(WCComic &$comic) {
		$out = array();

		//
		// @{
		$stmt = false;
		$stmtID = __CLASS__."::".__FUNCTION__;
		if($this->_db->has($stmtID)) {
			$stmt = $this->_db->get($stmtID);
		} else {
			$query = "select  cim_id\n";
			$query.= "from    {$this->_dbprefix}comic_images\n";
			$query.= "where   cim_group    = :group\n";
			$query.= " and    cim_comic    = :comic\n";

			$stmt = $this->_db->prepare($stmtID, $query);
		}
		// @}

		$params = array(
			":group" => $comic->group,
			":comic" => $comic->id()
		);

		if($stmt->execute($params) && $stmt->rowCount() > 1) {
			foreach($stmt->fetchAll() as $row) {
				$out[] = $this->item($row["cim_id"]);
			}

			uasort($out, "WCComicImage::PositionSort");
		}

		return $out;
	}
}
